<?php
/**
 * @package B-Index
 * @filesource gmap.class.php
 *
 *  Created on 28.09.2007 and (C) by Nikolai Plath
 */
class gmap {

    var $FNcheckMap = "
function checkMap()
{
	if (GBrowserIsCompatible())
	{
		if (tst)
			if (tst.offsetWidth != tst.getAttribute(\"oldValue\"))
			{
				tst.setAttribute(\"oldValue\",tst.offsetWidth);
				if (tst.getAttribute(\"refreshMap\")==0)
				if (tst.offsetWidth > 0)
				{
					clearInterval(tstint);
					getMap();
					tst.setAttribute(\"refreshMap\", 1);
				} 
			}
	}
}";
    
	var $addListenerMapClick = '
	var clicked;
	GEvent.addListener(map, "click", function(marker, point)
	{
		if (marker)
		{
  		} else
		{
			if (clicked != 1)
			{
				clicked = 1;
			  	var marker = new GMarker(point, {draggable: true});
			    map.addOverlay(marker);

				document.getElementById(\'gmap_lat\').value = point.lat();
				document.getElementById(\'gmap_lng\').value = point.lng();
	
				GEvent.addListener(marker, "dragend", function()
				{
				document.getElementById("gmap_lat").value = marker.getPoint().lat();
				document.getElementById("gmap_lng").value = marker.getPoint().lng();
				});
			}
		}			
	});';
		
	var $FNinitMap = "
\n\n<!-- G Map START -->\n
<script type='text/javascript'>//<![CDATA[\n
// global var linked to div
var tst=document.getElementById('gmap');
var tstint;

// Functions to watch if the map has changed
function checkMap()
{
	if (GBrowserIsCompatible())
	{
		if (tst)
			if (tst.offsetWidth != tst.getAttribute(\"oldValue\"))
			{
				tst.setAttribute(\"oldValue\",tst.offsetWidth);
				if (tst.getAttribute(\"refreshMap\")==0)
					if (tst.offsetWidth > 0) {
						clearInterval(tstint);
						getMap();
						tst.setAttribute(\"refreshMap\", 1);
					} //if
			} //if
	} //if
} //function checkMap

// Function for displaying the map and marker
function getMap()
{
	if (tst.offsetWidth > 0) {
	var map = new GMap2(document.getElementById('gmap'));
				
//The icon	
var baseIcon = new GIcon();
    baseIcon.iconSize=new GSize(32,32);
    baseIcon.shadowSize=new GSize(56,32);
    baseIcon.iconAnchor=new GPoint(16,32);
    baseIcon.infoWindowAnchor=new GPoint(16,0);
	   	
//the controls
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	map.addControl(new GScaleControl());
				";

//Here comes the mysterious Timeoutcode for FCKNG IE 
// Call the Maps through timeout to render in IE also
// Set an event for watching the changing of the map so it can refresh itself
	var $FNslowDownIE = //"<script type=\"text/javascript\">//<![CDATA[
"\nif (GBrowserIsCompatible()) {
	tst.setAttribute(\"oldValue\",0);
	tst.setAttribute(\"refreshMap\",0);
	tstint=setInterval(\"checkMap()\",500);
}
\n	";

	//InfoBox
	var $InfoBox = "
var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(460,20));
pos.apply(document.getElementById('gmap_infobox'));
map.getContainer().appendChild(document.getElementById('gmap_infobox'));
";

	var $MAP_Close = "
\n
//]]></script>

<!-- G Map END -->
\n";
	var $_CONF	= NULL; #EXTERNAL CONFIG

   function gmap( $external_config = NULL )
    {
    	/**
    	 * EXTERNAL CONFIG
    	 * 
    	 * expecting:
    	 * GMapKey			= the API key
    	 * GMapGBasePath	= Base path for icons
    	 * 
    	 * 	    $mapWidth = $biConfig->GMapMainWidth;//$botParams->get('width', '100%');
	    $mapHeight = $biConfig->GMapMainHeight;//$botParams->get('height', '500');
	    $centerPointLat = $biConfig->GMapMainCenterPointLat;//$botParams->get('centerPointLat', '0.0');
	    $centerPointLong = $biConfig->GMapMainCenterPointLng;//$botParams->get('centerPointLong', '0.0');
	    $MapsZoom = $biConfig->GMapMainZoom;//$botParams->get('MapsZoom', '1');

    	 * 
    	 * 
    	 */
    	global $biConfig;
    	
    	$this->_CONF = ($external_config) ? $external_config : $biConfig;
    
    } // function

	function addHeadTag ()
	{
		global $mainframe;
		$mainframe->addCustomHeadTag( "<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=".$this->_CONF->GMapKey."\" type=\"text/javascript\"></script>");
	} //function
	
	function checkForAPIkey()
	{
		if( !$this->_CONF->GMapKey | $this->_CONF->GMapKey == '0' )
		{
			echo '<span style="color: red;">'.BI_NO_API_KEY.'</span>';
			return FALSE;
		}
		return TRUE;
	} //function
	
	function drawGOG_IconPalette( $pal = 0 )
	{ 
		//global $biConfig;
		echo '<h1 align="center" style="padding-top:5px;">GoogleMaps Icons</h1>';
		echo '<p style="padding-left:10px;">Diese Icons werden von <strong>';
		echo( $pal > 0 ) ? $this->_CONF->GMapGBasePath : $this->_CONF->GMapCustomBasePath;
		echo '</strong> geladen</p>';
		$pal = intval($pal);
		if( $pal > 0 && $pal < 6 )
		{ $this->_drawGOG_pal( $pal );
		} else if( $pal == 0 )
		{	
			$this->_drawCustom_pal();
		} else
		{
			for ($pal = 2; $pal <= 5; $pal++)
			{ $this->_drawGOG_pal( $pal );
			}//for
			$this->_drawCustom_pal();
		} //if
	} //function
	
	function _drawCustom_pal ()
	{
		
		if( $handle = opendir( $this->_CONF->GMapCustomBasePath ) )
		{
			$icons_in_row = 5;
			?>
			<script type="text/javascript">
			function do_it(name)
			{
				opener.document.adminForm.gicon_pal.value='0';
				opener.document.adminForm.gicon_no.value='0';
				opener.document.adminForm.gicon_name.value=name;
				opener.document.adminForm.gicon_ico.src='<?php echo $this->_CONF->GMapCustomBasePath; ?>'+name;
				opener.focus();
				window.close();
			}
			</script>
			<?php
			$ix=1;
			$filenames = array();
			echo '<table width="100%" border="0" cellpadding="5px">' .
					'<tr>';
			while ($file = readdir($handle)) {
				if( $file <> "." && $file <> "..")
				{
					$filenames[] = $file;
					$JS = "onclick=\"do_it('".$file."')\"";
					echo '<td  class="g_select_icon" align="center" '.$JS.'>' .
								'<img src="'.$this->_CONF->GMapCustomBasePath.$file.'" alt ="icon" />' .
							'</td>';


					if( $ix == $icons_in_row )
					{
					 echo '</tr><tr>';
					 	$ix = 1;
					} else
					{
					 $ix++;
					} //if
				} //if
			} //while
				
			#filler...
			for( $addx = $ix; $addx <= $icons_in_row; $addx++) { echo '<td>&nbsp;</td>'; }
			// we are doing this to get a valid table...
			
			echo '</tr>' .
					'</table>';
		} //if
//$DBG->out($templates);
	# sort list of languages
#	sort($filenames);
#	reset($filenames);
// TODO we really should sort this..	
	
	
	/*
		$submit_action = "onchange=\"" .
//				"document.frmSelect.code_php.value=codearea_php.getCode(); " .
//				"document.frmSelect.code_css.value=codearea_css.getCode(); " .
//				"document.frmSelect.preview.value='yes'; " .
				"document.frmSelect.mode.value='".$mode."'; " .
				"document.frmSelect.template.value=document.frmSelect.template".$field_name.".value; " .
				"document.frmSelect.submit();" .
				"\"";
	*/
	
	//$ret = mosHTML::selectList( $templates, 'template'.$field_name, 'class="inputbox" size="1"'.$submit_action, 'value', 'text', $sel_item );
	
	//return ($ret);
		
		
		
		
	} //function
	
	/**
	 * to be displayed in a popup..
	 * setting information in opener window
	 * @param int $pal palette number
	 */
	function _drawGOG_pal ($pal)
	{
		$icons_in_row = 8;
		?>
		<script type="text/javascript">
		function do_it(num,name){
		//wahl=frm.typlist.options[frm.typlist.selectedIndex].text;
		opener.document.adminForm.gicon_pal.value=<?php echo $pal; ?>;
		opener.document.adminForm.gicon_no.value=num;
		opener.document.adminForm.gicon_name.value=name;
		opener.document.adminForm.gicon_ico.src='http://maps.google.com/mapfiles/kml/pal<?php echo $pal; ?>/icon'+num+'.png';
		
		opener.focus();
		window.close();
		}
		</script>

		<div style="border: 1px solid black;">
			<h3 align="center">Pal <span style="color: red;"><?php echo $pal; ?></span></h3>
		
			<table width="100%" border="0" cellpadding="5px">
			<?php	
			for ($ico = 0; $ico < $icons_in_row; $ico++)
			{
				echo "<tr>";
				for ($icox = 0; $icox < $icons_in_row; $icox++)
				{
					$icnum = $ico * $icons_in_row + $icox;
					$tmp_str = "";
					$JS = 'onclick="do_it('.$icnum.')"';
					$img_str = '<img src="'.$this->_CONF->GMapGBasePath.'pal'.$pal.'/icon'.$icnum.'.png" alt="icon" />';

/*
 * 
 * This one save all known google icons to files / also the shadows...
$tmp_str_s = "icon".$icnum."s.png";

ob_start();
$fp = fopen('http://maps.google.com/mapfiles/kml/pal'.$pal.'/'.$tmp_str, "rb");
fpassthru($fp);
fclose($fp);
$file = ob_get_contents();
ob_end_clean();
$fpx = fopen($biConfig->absolutePath.'/components/com_bindex/images/mapfiles/kml/pal'.$pal.'/'.$tmp_str, "w");
fwrite($fpx, $file);
fclose($fpx);

ob_start();
$fp = fopen('http://maps.google.com/mapfiles/kml/pal'.$pal.'/'.$tmp_str_s, "rb");
fpassthru($fp);
fclose($fp);
$file = ob_get_contents();
ob_end_clean();
$fpx = fopen($biConfig->absolutePath.'/components/com_bindex/images/mapfiles/kml/pal'.$pal.'/'.$tmp_str_s, "w");
fwrite($fpx, $file);
fclose($fpx);
*/
					
					echo '<td  class="g_select_icon" align="center" '.$JS.'>'.$img_str.'</td>';
				}//for
				echo '</tr>';
			}//for
			?>			
			</table>
		</div>
		<?php		
	} //function
	
		function _drawGIconBereich ( $id_bereich, $echo = TRUE )
	{
		$bereich = new bereich;
		$bereich->getFromID( $id_bereich );
		if( $bereich->icon_pal )
		{ 
			$text = '<img src="'.$this->_CONF->GMapGBasePath.'pal'.$bereich->icon_pal.'/icon'.$bereich->icon_no.'.png" alt="icon" border="0" />';
			if( $echo )
			{
				echo $text;
			} else
			{
				return $text;
			} //if
			//echo '<img src="http://maps.google.com/mapfiles/kml/pal'.$bereich->icon_pal.'/icon'.$bereich->icon_no.'.png" alt="icon" />';
		} elseif( $bereich->icon_name != '' )
		{ 
			$text = '<img src="'.$this->_CONF->GMapCustomBasePath.$bereich->icon_name.'" alt="icon" border="0" />';
			if( $echo )
			{
				echo $text;
			} else
			{
				return $text;
			} //if
			//echo '<img src="http://maps.google.com/mapfiles/kml/pal'.$bereich->icon_pal.'/icon'.$bereich->icon_no.'.png" alt="icon" />';
		} //if
		
	} //function
	
	function _drawGIconSelector ( $pal = 0, $no = 0, $name = NULL)
	{
		############################################
		# to be included in adminForm
		$ico_src = '';
		$link_base = 'index2.php?option=com_bindex&amp;task=show_gicons&amp;pal=';
		?>
		<div style="height: 0px;"><!-- we are hidden ;) -->
			<input type="text" name="gicon_pal" style="visibility: hidden;" value="<?php echo $pal; ?>" />
			<input type="text" name="gicon_no" style="visibility: hidden;" value="<?php echo $no; ?>" />
			<input type="text" name="gicon_name" style="visibility: hidden;" value="<?php echo $name; ?>" />
		</div>
		<table style="border: 1px solid black;" width="100%">
			<tr>
				<td align="center" width="10%" valign="center">
					<?php
					if( $pal > 0 && $no > 0 ) $ico_src = $this->_CONF->GMapGBasePath.'pal'.$pal.'/icon'.$no.'.png';
					if( $name != 'undefined' && $name != '' ) $ico_src = $this->_CONF->GMapCustomBasePath.$name;
					echo '<img name="gicon_ico" src="'.$ico_src.'" alt="icon" />';
					?>
				</td>
				<td align="left">
					<a href="<?php echo $link_base; ?>2" onclick="return popup(this,550,600)" title="Palette 2">Palette 2</a>
					&nbsp;|&nbsp
					<a href="<?php echo $link_base; ?>3" onclick="return popup(this,550,600)" title="Palette 3">Palette 3</a>
					&nbsp;|&nbsp
					<a href="<?php echo $link_base; ?>4" onclick="return popup(this,550,600)" title="Palette 4">Palette 4</a>
					&nbsp;|&nbsp
					<a href="<?php echo $link_base; ?>5" onclick="return popup(this,550,600)" title="Palette 5">Palette 5</a>
					<br />
					<a href="<?php echo $link_base; ?>0" onclick="return popup(this,550,600)" title="Custom">Custom</a>
				</td>
			</tr>
		</table>
		<?php
		
		# adminForm
		############################################ 
	} //function
	

	function _drawGMapSelector( $item = NULL )
	{
		global $mosConfig_live_site;
		global $database, $mainframe;

		if( ! $this->checkForAPIkey() ) return;
		$this->echo_NO_JS_WARNING();

		$repl = '';
		$Markers = '';
		$MarkerString = '';
		
		$this->addHeadTag();

		$MarkerString .= $this->addListenerMapClick;
		
		if( $item && $item->gmap_lat > 0 )
		{
			$BiItem = new biItem($item, $item);
			$bereich = new bereich;
			$bereich = getBereichFromIDstadt( $item->id_stadt );
			$icon_pal 	= $bereich->icon_pal;
			$icon_no 	= $bereich->icon_no;
			$icon_name	= $bereich->icon_name;

			$Markers .= 'var posn = new GLatLng('.$item->gmap_lat.', '.$item->gmap_lng.');'."\n";

			if ( $icon_pal < 2 )
			{
				if( $icon_name != '' )
				{ ### custom marker
	    			$img = $mosConfig_live_site.'/'.$this->_CONF->GMapCustomBasePath.$icon_name;
			      $dims = getimagesize($img);
			      $Markers .= "var icon = new Array(\"$img\",$dims[0],$dims[1]);\n";

					$Markers .= "
						var tmp=new GIcon();
						tmp.image=icon[0];
						tmp.iconSize=new GSize(icon[1],icon[2]);
						subt=icon[2]/100*11;
						subt=Math.ceil(subt);
						var yps=icon[2]-subt;
						tmp.iconAnchor=new GPoint(10,yps);
						tmp.infoWindowAnchor=new GPoint(30,yps-50);
						var marker=new GMarker(posn,tmp);
					";
				} else
				{
					$path_icon		= $this->_CONF->GMapGBasePath.'pal2/icon5.png';
					$path_icon_s	= $this->_CONF->GMapGBasePath.'pal2/icon5s.png';
					$Markers .= "var icon = new GIcon(baseIcon, '".$path_icon."', null, '".$path_icon_s."');";
					$Markers .= "var marker = new GMarker(posn, icon);\n";
				}
	
			} else
			{
				$path_icon		= $this->_CONF->GMapGBasePath.'pal'.$bereich->icon_pal.'/icon'.$bereich->icon_no.'.png';
				$path_icon_s	= $this->_CONF->GMapGBasePath.'pal'.$bereich->icon_pal.'/icon'.$bereich->icon_no.'s.png';
				$Markers .= "var icon = new GIcon(baseIcon, '".$path_icon."', null, '".$path_icon_s."');\n";
				$Markers .= "var marker = new GMarker(posn, icon);";
			}// if
	
			#show blowup
			$Markers .= "GEvent.addListener(marker,\"click\",function() { map.showMapBlowup(marker.getPoint()); } );";

			#not used..
			if(false){
							$TabEinsContent = $this->_UserTab($BiItem, 1);
							$TabZweiContent = $this->_UserTab($BiItem, 2);
							$Markers .= "var infoTabs = [
								new GInfoWindowTab(\"".htmlspecialchars(BI_MAP_TAB_ONE)."\", \"".$TabEinsContent."\"),
								new GInfoWindowTab(\"".htmlspecialchars(BI_MAP_TAB_TWO)."\", \"".$TabZweiContent."\")
								];";
							
							$Markers .="
							GEvent.addListener(marker, 'click', function() {
							marker.openInfoWindowTabsHtml(infoTabs);
							});";
			}// if
			$Markers .= "map.addOverlay(marker);\n";
		}//if (lat >0)
		
		$MarkerString .= $Markers;

   	$repl .= '<div id="gmap" style="width: 100%; height: 400px"></div>';
	
		$repl .= $this->FNinitMap;
	
		//set the center
		
		if ( $item && $item->gmap_lat )
		{	//Set the center to known location
			$repl .= 'map.setCenter(new GLatLng('.$item->gmap_lat.', '.$item->gmap_lng.'), 12);';
		} else {   //Set the center to "world view"
			$repl .= "map.setCenter(new GLatLng(52.280721, 12.889567), 8);";
	
		}//if
	
		$repl .= $MarkerString;
	 
		 //CONCLUSION !!!
		$repl .= '}//if (tst.offsetWidth > 0)
			}//function';
	
		$repl .= $this->FNslowDownIE;
		$repl .= $this->MAP_Close;
		?>
	
<table style="border: 1px solid black;" width="100%">
	<tr>
		<td colspan="2"><?php echo $repl; ?></td>
	</tr>
	<tr align="right">
		<td>Lat <input type="text" name="gmap_lat" id="gmap_lat" size="25" value="<?php echo ($item) ? $item->gmap_lat : ''; ?>" /></td>
	
		<td>Lng <input type="text" name="gmap_lng" id="gmap_lng" size="25" value="<?php echo ($item) ? $item->gmap_lng : ''; ?>" /></td>
	</tr>
</table>

	<?php
	} //function
	
	function g_map ( $id_bereich = 0, $id_stadt = 0)
	{
		global $mainframe, $database, $mosConfig_lang;
		global $biConfig;
global $DBG;

		if( !$biConfig->GMapKey | $biConfig->GMapKey == '0' )
		{
			$repl = '<span style="color: red;">'.BI_NO_API_KEY.'</span>';
			return;
		} //if

		echo '<noscript><br /><h1 style="color:red;">No Javascript - No Googlemap :-P</h1></noscript>';


	    $mapWidth = $biConfig->GMapMainWidth;//$botParams->get('width', '100%');
	    $mapHeight = $biConfig->GMapMainHeight;//$botParams->get('height', '500');
	    $centerPointLat = $biConfig->GMapMainCenterPointLat;//$botParams->get('centerPointLat', '0.0');
	    $centerPointLong = $biConfig->GMapMainCenterPointLng;//$botParams->get('centerPointLong', '0.0');
	    $MapsZoom = $biConfig->GMapMainZoom;//$botParams->get('MapsZoom', '1');
		
		$GMapBasePath = "http://maps.google.com/mapfiles/kml/";

		$mainframe->addCustomHeadTag( "<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=".$biConfig->GMapKey."\" type=\"text/javascript\"></script>");
/*

	    $biConfig->GMapMainCenterPointLat =  '50.48547354578499';
	    $biConfig->GMapMainCenterPointLng = '10.2392578125';
	    $biConfig->GMapMainZoom = '6';
*/



	$Title = '';


	$where2 = '';
	if( $id_bereich > 0 )
	{
		$where2 = "\n AND `id_stadt` IN (" .
				"\n SELECT `id` FROM `#__bindex_staedte`" .
				"\n WHERE `id_bereich` = '".$id_bereich."')" .
				"\n ORDER BY `id_stadt`";
		$bereich = new bereich( $id_bereich, $mosConfig_lang );
		$Title = '<div class="gmap_header">'.$this->_drawGIconBereich($id_bereich, FALSE).'&nbsp;&nbsp;'.$bereich->getNameOrDefault($mosConfig_lang).'</div>';
	} elseif( $id_stadt > 0)
	{
		$where2 = "\n AND `id_stadt` = '".$id_stadt."'";
		$Title = '<div class="gmap_header">'.getValueStadtOrDefault($id_stadt,$mosConfig_lang).'</div>';
	} //if

	$query = "SELECT *" .
			"\n FROM `#__bindex_items`" .
			"\n WHERE `published` = '1'"
			. $where2;

$DBG->query($query);

	$database->setQuery($query);
	$Bs = $database->loadObjectList();

	if ($database->getErrorNum())
	{ echo $database->stderr();
		return;
	}




/*



		$Bs_add = array();
		foreach( $Bs as $B )
		{
			$i = new bindex($database);
			$query = "SELECT * FROM `#__bindex_items`" .
					"\n WHERE `id` = '{$B->id_item}'" .
					"\n LIMIT 1";
			$database->setQuery($query);
			
			$database->loadObject($i);
$DBG->out($B);
//$DBG->out('B'.$B->id_stadt ." - I". $i->id_stadt);
			$B->id_stadt = $i->id_stadt;
			$B->gmap_lat = $i->gmap_lat;
			$B->gmap_lng = $i->gmap_lng;
			$Bs_add[] = $B;
$DBG->out($B);
$DBG->out('B'.$B->id_stadt ." - I". $i->id_stadt);
$DBG->query($query);
		} //foreach





		global $database;
		$query = "SELECT * FROM #__sobi2_item";
		$database->setQuery( $query );
		$users = $database->loadObjectList();

		$selectedusers = count($users);
		
		if ($selectedusers > 0)
		{

			define ( '_TAB_EINS_TEXT', 'Contacto' );//ins Langfile
			define ( '_TAB_ZWEI_TEXT', 'Infos' );//ins Langfile

			$FullEditString ='';
*/
			$Markers = '';

			
						
			$cnt=0;

			foreach( $Bs as $it )
			{
/*						
			foreach($users as $user)
			{
				$u_name = $user->title;
				$query1 = "SELECT * FROM #__sobi2_fields_data WHERE itemid = ".$user->itemid;
				$database->setQuery( $query1 );
				$users_data = $database->loadObjectList();

				$query2 = "SELECT catid FROM #__sobi2_cat_items_relations WHERE itemid = ".$user->itemid;
				$database->setQuery( $query2 );
				$u_catid = $database->loadResult();
		
				$query3 = "SELECT * FROM #__sobi2_categories WHERE catid = " . $u_catid;
				$database->setQuery( $query3 );
				$a_u_category = $database->loadObjectList();
				$u_category = $a_u_category[0]->name;
				//echo "<h1>pat:".$a_u_category[0]->gmap_icon_pal."</h1>";
				
				$Sobi2ItemLink = "index.php?option=com_sobi2&amp;sobi2Task=sobi2Details&amp;catid=" . $u_catid . "&amp;sobi2Id=" . $user->itemid . "&amp;Itemid=9999999";


				$u_lat = 0;
				$u_lon = 0;
				$u_str = '';


				foreach($users_data as $u_data)
				{
					switch ($u_data->fieldid)
					{
						case '1'://strasse
							$u_str = $u_data->data_txt;
						break;
						case '8'://Website
							$u_web = $u_data->data_txt;
						break;
						case $config->googleMapsLatField:
							$u_lat = $u_data->data_txt;
						break;
						case $config->googleMapsLongField:
							$u_lon = $u_data->data_txt;
						break;
					}//switch ($u_data->fieldid)
			
				}//foreach($users_data as $u_data)
*/

				if ($it->gmap_lat >0)
				{
$DBG->out($it);
					$cnt++;
					$bereich = new bereich;
					$bereich = getBereichFromIDstadt( $it->id_stadt );
					$icon_pal 	= $bereich->icon_pal;
					$icon_no 	= $bereich->icon_no;

					if ( $icon_pal < 2 )
					{
						$icon_pal = 2;
						$icon_no = 5;
					}

					$path_icon		= $GMapBasePath.'pal'.$icon_pal.'/icon'.$icon_no.'.png';
					$path_icon_s	= $GMapBasePath.'pal'.$icon_pal.'/icon'.$icon_no.'s.png';

					$Markers .= 'var posn = new GLatLng('.$it->gmap_lat.', '.$it->gmap_lng.');';
					$Markers .= "var icon".$cnt." = new GIcon(baseIcon, '".$path_icon."', null, '".$path_icon_s."');";
					$Markers .= "var marker".$cnt." = new GMarker(posn, icon".$cnt.");";

					#Get user info
					
					$item = new biItem( bindex::get($it->id, $mosConfig_lang), bindex::get($it->id, $biConfig->ini_language) );
$DBG->out($item);					
					
					#Filling tabs with user info
					$TabEinsContent = $this->_UserTab($item, 1);
					//$TabEinsContent = "Hi";//"<a href=". $Sobi2ItemLink . ">".$u_name. "</a><br />"
						//.$u_str."<br />";
					$TabZweiContent = $this->_UserTab($item, 2);//"<strong>Website:</strong><br /><a href='http://".$ttemp[0]."' target='_blank' >".$ttemp[1]."</a>";

					$Markers .= "var infoTabs".$cnt." = [
						new GInfoWindowTab(\"".htmlspecialchars(BI_MAP_TAB_ONE)."\", \"".$TabEinsContent."\"),
						new GInfoWindowTab(\"".BI_MAP_TAB_TWO."\", \"".$TabZweiContent."\")
						];";
					
					$Markers .="
					GEvent.addListener(marker".$cnt.", 'click', function() {
					marker".$cnt.".openInfoWindowTabsHtml(infoTabs".$cnt.");
					});
					map.addOverlay(marker".$cnt.");\n";
					}//if ($u_lat >0)



			}//foreach($users as $user)
/*
				}//if ($selectedusers > 0)
*/



#putting it all together..
		$repl = '';

#The title
		$repl .= $Title;

	// The Map "Window"
		$repl .= '<div id="gmap" style="width: 100%; height: 600px;"></div>';//'"'.$mapWidth.'; height: '.$mapHeight.';"></div>';
	// The Infobox
		$repl .= '<div id="gmap_infobox" align="left">Brought to you by:<br /><a href="http://www.nik-it.de">NiK-IT.de</a></div>';
	// me make the map ...
		$repl .= "<script type='text/javascript'>//<![CDATA[\n";
	// global var linked to div
		$repl .="var tst=document.getElementById('gmap');
				 var tstint;
				 ";
	// Functions to watch if the map has changed
		$repl .= "function checkMap()
			{
				if (GBrowserIsCompatible())
				{
					if (tst)
						if (tst.offsetWidth != tst.getAttribute(\"oldValue\"))
						{
							tst.setAttribute(\"oldValue\",tst.offsetWidth);

							if (tst.getAttribute(\"refreshMap\")==0)
							if (tst.offsetWidth > 0) {
								clearInterval(tstint);
								getMap();
								tst.setAttribute(\"refreshMap\", 1);
							} 
						}
				}
			}";
	// Function for displaying the map and marker
		$repl .= "function getMap()
			{
				if (tst.offsetWidth > 0) {
				var map = new GMap2(document.getElementById('gmap'));
					";
	//The icon	
		$repl .= "
		var baseIcon = new GIcon();
          baseIcon.iconSize=new GSize(32,32);
          baseIcon.shadowSize=new GSize(56,32);
          baseIcon.iconAnchor=new GPoint(16,32);
          baseIcon.infoWindowAnchor=new GPoint(16,0);";
	   	
	//the controls
		$repl .= "map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				map.addControl(new GOverviewMapControl());
				map.addControl(new GScaleControl());";

	//set the center
		$repl .= "map.setCenter(new GLatLng( $biConfig->GMapMainCenterPointLat, $biConfig->GMapMainCenterPointLng ), $biConfig->GMapMainZoom);";
	
	//InfoBox
	$InfoBox = "var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(460,20));
      pos.apply(document.getElementById('gmap_infobox'));
      map.getContainer().appendChild(document.getElementById('gmap_infobox'));";

	$repl .= $InfoBox;
	$repl .= $Markers;	
	$repl .= "}
		}
		//]]></script>
		";

////Here comes the mysterious Timeoutcode for FCKNG IE #*+-
		// Call the Maps through timeout to render in IE also
		// Set an event for watching the changing of the map so it can refresh itself
	
	$repl .= "<script type=\"text/javascript\">//<![CDATA[\n";
			
	$repl .="if (GBrowserIsCompatible()) {
				tst.setAttribute(\"oldValue\",0);
				tst.setAttribute(\"refreshMap\",0);
				tstint=setInterval(\"checkMap()\",500);
			}
		//]]></script>
		";
		echo $repl;
	} //function







	/**
	 * Echo out a large warning if javascript is disabled
	 */
	function echo_NO_JS_WARNING ()
	{
		echo '<noscript><br /><h1 style="color:red;">No Javascript - No Googlemap :-P</h1></noscript>';
	} //function


/**
 * gmapTitle		# Title including HTML
 * gmapWidth		# in % or px - nothing sets px
 * gmapHeight		# in px - nothing sets px
 * gmapCenterLat
 * gmapCenterLng
 * gmapZoom
 */


	function g_map2 ( $id_bereich = 0, $id_stadt = 0)
	{
		global $mainframe, $database, $mosConfig_lang;
		global $biConfig;
global $DBG;

		if( ! $this->checkForAPIkey() ) return;
		$this->echo_NO_JS_WARNING();


/*		
	    $mapWidth = $biConfig->GMapMainWidth;//$botParams->get('width', '100%');
	    $mapHeight = $biConfig->GMapMainHeight;//$botParams->get('height', '500');
	    $centerPointLat = $biConfig->GMapMainCenterPointLat;//$botParams->get('centerPointLat', '0.0');
	    $centerPointLong = $biConfig->GMapMainCenterPointLng;//$botParams->get('centerPointLong', '0.0');
	    $MapsZoom = $biConfig->GMapMainZoom;//$botParams->get('MapsZoom', '1');
*/		
		$GMapBasePath = $this->_CONF->GMapGBasePath;//"http://maps.google.com/mapfiles/kml/";
		
		$this->addHeadTag();

		$Title = '';
		$where2 = '';
	
		if( $id_bereich > 0 )
		{
			$where2 = "\n AND `id_stadt` IN (" .
					"\n SELECT `id` FROM `#__bindex_staedte`" .
					"\n WHERE `id_bereich` = '".$id_bereich."')" .
					"\n ORDER BY `id_stadt`";
			$bereich = new bereich( $id_bereich, $mosConfig_lang );
			$Title = '<div class="gmap_header">'.$this->_drawGIconBereich($id_bereich, FALSE).'&nbsp;&nbsp;'.$bereich->getNameOrDefault($mosConfig_lang).'</div>';
		} elseif( $id_stadt > 0)
		{
			$o_id_bereich = getBereichFromIDstadt( $id_stadt );
			$id_bereich = $o_id_bereich->id;
			$bereich = new bereich( $id_bereich, $mosConfig_lang );
			$Title = '<div class="gmap_header">'.$this->_drawGIconBereich($id_bereich, FALSE).'&nbsp;&nbsp;'.$bereich->name.' / '.getValueStadtOrDefault($id_stadt,$mosConfig_lang).'</div>';
			$where2 = "\n AND `id_stadt` = '".$id_stadt."'";
		} //if
	
		$query = "SELECT *" .
				"\n FROM `#__bindex_items`" .
				"\n WHERE `published` = '1'"
				. $where2;
	
$DBG->query($query);
	
		$database->setQuery($query);
		$Bs = $database->loadObjectList();
	
		if ($database->getErrorNum())
		{ echo $database->stderr();
			return;
		}

#putting it all together..
		$repl = '';

#The title
		$repl .= $Title;

	// The Map "Window"
		$repl .= '<div id="gmap" style="width: '.$this->_CONF->GMapMainWidth.'; height: '.$this->_CONF->GMapMainHeight.';"></div>';
	// The Infobox
		$repl .= '<div id="gmap_infobox" align="left">Brought to you by:<br /><a href="http://www.nik-it.de">NiK-IT.de</a></div>';

	// me make the map ...

		$repl .= $this->FNinitMap;

	//set the center
		$repl .= '
				map.setCenter(new GLatLng( '.$this->_CONF->GMapMainCenterPointLat.' , '. $this->_CONF->GMapMainCenterPointLng.' ), '.$this->_CONF->GMapMainZoom.');';

	$repl .= $this->InfoBox;//$InfoBox;

# Get Markers
	
		$Markers = $this->_JSmarkers($Bs);

	$repl .= $Markers;	
	$repl .= "}
		}
		";
	
	$repl .= $this->FNslowDownIE;
	
	$repl .= $this->MAP_Close;

		echo $repl;
	} //function

/**
 * @param object array $Bs array of bindex objects
 * 
 * @return JS Marker positions
 */
function _JSmarkers( $Bs )
{
	global $mosConfig_lang, $mosConfig_live_site;
	global $biConfig;
global $DBG;
		
			
			$cnt=0;
			$Markers = '';
			foreach( $Bs as $it )
			{
				if ($it->gmap_lat >0)
				{
$DBG->out($it);
					$cnt++;
					$bereich = new bereich;
					$bereich = getBereichFromIDstadt( $it->id_stadt );
					$icon_pal 	= $bereich->icon_pal;
					$icon_no 	= $bereich->icon_no;
					$icon_name	= $bereich->icon_name;

 			      $Markers .= "var posn = new GLatLng(\"".$it->gmap_lat."\",\"".$it->gmap_lng."\");\n";

					if ( $icon_pal < 2 )
					{
						if( $icon_name != '' )
						{ ### custom marker

	    					$img = $mosConfig_live_site.'/'.$this->_CONF->GMapCustomBasePath.$icon_name;
			            $dims = getimagesize($img);
			            $Markers .= "var icon = new Array(\"$img\",$dims[0],$dims[1]);\n";
$Markers .= "

	var tmp=new GIcon();
	tmp.image=icon[0];
	tmp.iconSize=new GSize(icon[1],icon[2]);
	subt=icon[2]/100*11;
	subt=Math.ceil(subt);
	var _4=icon[2]-subt;
	tmp.iconAnchor=new GPoint(10,_4);
	tmp.infoWindowAnchor=new GPoint(30,_4-50);
	var marker".$cnt."=new GMarker(posn,tmp);
";

						} else
						{
							$path_icon		=  $this->_CONF->GMapGBasePath.'pal2/icon5.png';
							$path_icon_s	=  $this->_CONF->GMapGBasePath.'pal2/icon5s.png';
							$Markers .= "var icon".$cnt." = new GIcon(baseIcon, '".$path_icon."', null, '".$path_icon_s."');";
							$Markers .= "var marker".$cnt." = new GMarker(posn, icon".$cnt.");\n";
						}// if
					} else
					{
						$path_icon		=  $this->_CONF->GMapGBasePath.'pal'.$icon_pal.'/icon'.$icon_no.'.png';
						$path_icon_s	=  $this->_CONF->GMapGBasePath.'pal'.$icon_pal.'/icon'.$icon_no.'s.png';
						$Markers .= "var icon".$cnt." = new GIcon(baseIcon, '".$path_icon."', null, '".$path_icon_s."');\n";
						$Markers .= "var marker".$cnt." = new GMarker(posn, icon".$cnt.");\n";
						
					}

					#Get user info
					
					$item = new biItem( bindex::get($it->id, $mosConfig_lang), bindex::get($it->id, $biConfig->ini_language) );
$DBG->out($item);					
					
					#Filling tabs with user info
					$TabEinsContent = $this->_UserTab($item, 1);
					$TabZweiContent = $this->_UserTab($item, 2);

					$Markers .= "var infoTabs".$cnt." = [
						new GInfoWindowTab(\"".htmlspecialchars(BI_MAP_TAB_ONE)."\", \"".$TabEinsContent."\"),
						new GInfoWindowTab(\"".htmlspecialchars(BI_MAP_TAB_TWO)."\", \"".$TabZweiContent."\")
						];";
					
					$Markers .="
					GEvent.addListener(marker".$cnt.", 'click', function() {
					marker".$cnt.".openInfoWindowTabsHtml(infoTabs".$cnt.");
					});
					map.addOverlay(marker".$cnt.");\n";
					}//if

			}//foreach

	return ($Markers);

} //function

	
	/**
	 * @param object $item as biItem
	 * @param int $tabname Tabno to display... current: 1, 2
	 * @return str HTML usertab content
	 */
	function _UserTab ( $item, $tabno = 1, $in_popup = FALSE )
	{
		global $Itemid;

		$ret = '';
		
		switch( $tabno )
		{
			case 1 :
				$link = 'index.php?option=com_bindex&amp;task=view&amp;Itemid='.$Itemid;
				if( $in_popup ) $link = 'index2.php?option=com_bindex&amp;task=view&amp;Itemid='.$Itemid;
				$ret .= 
						'<table>' .
						'<tr>' .
						'<td>' . $item->image(true,true) . '</td>' .
							'<td>' .
						'<strong>'.$item->name(TRUE, $link.'&amp;bid='.$item->B_ref->id_item).'</strong><br />';
				$ret .= $item->street(TRUE).' '.$item->street_number(TRUE).'<br />' .
						$item->city(TRUE).'</td>' .
						'</tr>' .
						'</table>';
			break;
			case 2:
				$ret .= $item->description(TRUE);
			break;
		} //switch

		return ($ret);
	}
	
} //class
?>